#include "comm.hpp"

int main()
{
    //1.创建key
    key_t key = ftok(PROC_PATH, PROJ_ID);
    assert(key != -1);
    std::cout << "--ftok success--" << std::endl;
    sleep(2);

    //2.创建共享内存
    int shmId = shmget(key, SHM_SIZE, 0);
    assert(shmId != -1);
    std::cout << "--shmget success--" << std::endl;
    sleep(2);

    //3.进程attach共享内存
    char* pshm = (char*)shmat(shmId, nullptr, 0);
    assert(pshm != (char*)-1);
    std::cout << "--shmat success--" << std::endl;
    sleep(5);

    //4.通信-客户端写
    while(true)
    {
        std::cout << "Please input:" << std::endl;
        size_t len = read(0, pshm, SHM_SIZE - 1);
        if(len > 0)
        {
            pshm[len - 1] = 0;//字符串末尾加上\0
            if(strcmp(pshm, "quit") == 0)
            {
                std::cout << "--client quit!!--" << std::endl;
                break;
            }
        }
        
    }


    //5.进程detach共享内存
    int n = shmdt(pshm);
    assert(n != -1);
    std::cout << "--shmdt success--" << std::endl;
    sleep(2);

    return 0;
}